//
//  SearchTableViewCell.m
//  Taste
//
//  Created by 徐昊 on 16/9/14.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "SearchTableViewCell.h"
#import "SearchRes.h"
#import "SearchNames.h"
#import "SearchCollectionViewCell.h"
#import "HXMacro.h"
#import "HomePageSearchViewController.h"
#import "HomPageSearchResultViewController.h"

#define kHomePageSearchCollectionViewCellName @"HomePageSearchCollectionViewCellNameReuse"
@interface SearchTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;

@end

@implementation SearchTableViewCell

- (void)setRes:(SearchRes *)res {
    _res = res;
    [_collectionView reloadData];
}

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    _layout.minimumInteritemSpacing = 2;
    _layout.minimumLineSpacing = 2;
    _layout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView.scrollEnabled = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor colorWithRed:0.883 green:1.000 blue:0.976 alpha:1.000];
    [_collectionView registerNib:[UINib nibWithNibName:@"SearchCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kHomePageSearchCollectionViewCellName];
    [self.contentView addSubview:_collectionView];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 10) / 3, 30);
    return _layout.itemSize;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _res.names.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHomePageSearchCollectionViewCellName forIndexPath:indexPath];
    cell.label.text = _res.names[indexPath.row].name;
    return cell;
}
// 实现点击标签搜索
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HomPageSearchResultViewController *vc = [[HomPageSearchResultViewController alloc] init];
    HomePageSearchViewController *homeVC = (HomePageSearchViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
    vc.flag = @"2";
    vc.searchString = _res.names[indexPath.row].name;
    if (indexPath.section == 0) {
        vc.cid = 1;
    } else {
        vc.cid = 14;
    }
    [homeVC.navigationController pushViewController:vc animated:YES];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
